package com.jet.core.utils;

import java.util.Stack;

public class CodeGenUtil {

    private static final char[] array1 = {
            'P', 'm', 'A', 'b', 'I', 'c', 'J', 'z', 'H', 'd',
            'a', '6', 'p', 'W', 'O', 'e', 'Q', 'f', '7', 'K',
            'h', 'C', 'i', '5', 'j', 'R', 'g', 'D', 'k', 'Z',
            'M', '8', 'n', '4', 'F', 'L', 'E', 'o', '9', 'X',
            'r', 'T', 's', 'B', '3', 't', 'V', 'G', 'u', 'Y',
            'v', '2', 'w', 'x', '1', 'y', 'U', '0', 'N', 'l',
            'S', 'q'
    };

    private static final char[] array2 = {
            'q', 'w', 'e', 'r', 't', 'Y', 'U', 'I', 'O', 'P',
            'a', 's', 'd', 'f', 'g', 'H', 'J', 'K', 'L',
            'z', 'x', 'c', 'v', 'B', 'N', 'M',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'Q', 'W', 'E', 'R', 'T', 'y', 'u', 'i', 'o', 'p',
            'A', 'S', 'D', 'F', 'G', 'h', 'j', 'k', 'l',
            'Z', 'X', 'C', 'V', 'b', 'n', 'm'
    };

    private static final char[] array3 = {
            '0', '6', '9', '8', '3', '7', '4', '2', '5', '1',
    };
    
    /**
     * 只包含字母
     *
     * @param number
     * @return
     */
    public static Long convertNumberCode(long number) {
        String result = convert(array3, number);
        return Long.valueOf(result);
    }

    /**
     * 包含数字和大小写字母
     *
     * @param number
     * @return
     */
    public static String convert(long number) {
        return convert(array1, number);
    }

    private static String convert(char[] array, long number) {
        long rest = number;
        Stack<Character> stack = new Stack<>();
        StringBuilder result = new StringBuilder(10);
        do {
            stack.add(array[Long.valueOf((rest - (rest / array.length) * array.length)).intValue()]);
            rest = rest / array.length;
        } while (rest != 0);

        while (!stack.isEmpty()) {
            result.append(stack.pop());
        }

        return result.toString();
    }
}
